package com.shaoshaohuo.app.ui;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

import com.shaoshaohuo.app.R;
import com.shaoshaohuo.app.constant.ExtraName;
import com.shaoshaohuo.app.entity.AccountDetailEntity;
import com.shaoshaohuo.app.entity.AccountDetailEntity.AccountDetail;
import com.shaoshaohuo.app.framework.SharedPreferencesHelper;
import com.shaoshaohuo.app.entity.BaseEntity;
import com.shaoshaohuo.app.entity.CheckPayPwdEntity;
import com.shaoshaohuo.app.net.RequestService;
import com.shaoshaohuo.app.net.HttpRequest.RequestListener;
import com.shaoshaohuo.app.view.TopbarView;
import com.shaoshaohuo.app.wxapi.RechangeActivity;

public class MyWalletActivity extends BaseActivity implements OnClickListener {

	private TopbarView mTopbarView;

	private Button mRechangeButton;
	private Button mWithdrawButton;
	
	private TextView mBalanceText;

	private View mShouzhimingxiLayout;
//	private View mCardManageLayout;
	private View mPayManageLayout;
	private View mConponLayout;

	private AccountDetail data;
	
	private boolean isHavePasswd = false;

	private TextView tv_modify_password;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_my_wallet);
		initView();
		setUpView();
	}
	

	@Override
	protected void onResume() {
		super.onResume();
		getAccountDetail();
		initModifyPassword();
	}
	
	/**
	 * 判断是否设置了 支付密码
	 */
	private void initModifyPassword() {
		String isHavePasswd = SharedPreferencesHelper.getString("isHavePasswd", "-1");
		
		if("1".equals(isHavePasswd)){
			isSetPassword();
		}else if("0".equals(isHavePasswd) ){
			isNoSetPassword();
		}else if("-1".equals(isHavePasswd)){
			RequestService.getInstance().checkPayPwd(this, CheckPayPwdEntity.class, new RequestListener() {
		        @Override
		        public void onSuccess(int requestCode, BaseEntity resultData) {
		            if(resultData.isOk()){
		                CheckPayPwdEntity entity = (CheckPayPwdEntity) resultData;
		                String isset = entity.getData().getIsset();
		                if("1".equals(isset)){
		                	isSetPassword();
		                }else if("0".equals(isset)){
		                	isNoSetPassword();	
		                }
		                SharedPreferencesHelper.setString("isHavePasswd", isset);
		            }
		        } 
		        @Override
		        public void onFailed(int requestCode, Exception error, String msg) {
		        }
		    });
		}
	}
	//如果设置了支付密码
	public void isSetPassword(){
		tv_modify_password.setText("修改支付密码");
		isHavePasswd = true;
	}
	//如果没有设置支付密码
	public void isNoSetPassword(){
		tv_modify_password.setText("设置支付密码");
		isHavePasswd = false;
	}
	
	private void getAccountDetail() {
//		startLoadingDialog();
		RequestService.getInstance().getAccountDetail(this, AccountDetailEntity.class, new RequestListener() {
			
			@Override
			public void onSuccess(int requestCode, BaseEntity resultData) {
				dismissLoadingDialog();
				if(resultData.isOk()){
					AccountDetailEntity entity = (AccountDetailEntity) resultData;
					updateUi(entity);
				}else{
					showToast(resultData.getMsg());
				}
			}
			
			@Override
			public void onFailed(int requestCode, Exception error, String msg) {
				dismissLoadingDialog();
				showToast(R.string.please_check_network);
			}
		});
	}
	
	protected void updateUi(AccountDetailEntity entity) {
		data = entity.getData();
		mBalanceText.setText("￥"+data.getFreetotal());
	}

	private void initView() {
		mTopbarView = (TopbarView) this.findViewById(R.id.topbar);

		mRechangeButton = (Button) this.findViewById(R.id.button_rechange);
		mWithdrawButton = (Button) this.findViewById(R.id.button_withdraw);

		mShouzhimingxiLayout = this.findViewById(R.id.layout_shouzhimingxi);
//		mCardManageLayout = this.findViewById(R.id.layout_yinhangka);
		mPayManageLayout = this.findViewById(R.id.layout_zhifuguanli);
		tv_modify_password = (TextView) findViewById(R.id.tv_modify_password);
		mConponLayout = this.findViewById(R.id.layout_youhuiquan);
		mBalanceText = (TextView) this.findViewById(R.id.textview_balance);
	}

	private void setUpView() {
		mTopbarView.setCenterText("我的钱包");
		mTopbarView.setLeftView(true, true);

		mRechangeButton.setOnClickListener(this);
		mWithdrawButton.setOnClickListener(this);
		mShouzhimingxiLayout.setOnClickListener(this);
//		mCardManageLayout.setOnClickListener(this);
		mPayManageLayout.setOnClickListener(this);
		mConponLayout.setOnClickListener(this);

	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.button_rechange:
			Intent intent1 = new Intent(this,RechangeActivity.class);
			startActivity(intent1);
			break;
		case R.id.button_withdraw:
			if(data!=null){
				Intent intent2 = new Intent(this,WithdrawActivity.class);
				intent2.putExtra(ExtraName.freetotal, data.getFreetotal());
				startActivity(intent2);
			}else{
				showToast(R.string.please_check_network);
			}
			break;
		case R.id.layout_shouzhimingxi:
			Intent intent3 = new Intent(this,IncomeStatementActivity.class);
			startActivity(intent3);
			break;
//		case R.id.layout_yinhangka:
//			Intent intent4 = new Intent(this,BankCardManageActivity.class);
//			startActivity(intent4);
//			break;
		case R.id.layout_zhifuguanli:
			Intent intent5 = new Intent(this,PayManageActivity.class);
			intent5.putExtra(ExtraName.isHavePasswd, isHavePasswd);
			startActivity(intent5);
			break;
		case R.id.layout_youhuiquan:
			Intent intent6 = new Intent(this,CouponActivity.class);
			startActivity(intent6);
			break;
		}
	}

}
